package com.huike.game.fragment;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.huike.game.Activity.GameActivity20;
import com.huike.game.R;
import com.huike.game.api.Api;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.fragment.app.Fragment;

public class Game22_set extends Fragment {

    Context context;
    private RequestQueue queue;
    private EditText editText;
    private  TextView tv_game22_desc;
    private String val;
    private   NotificationManager manager;
    private Timer timer;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        queue  = Volley.newRequestQueue(context);

        SharedPreferences sp = context.getSharedPreferences("price", Context.MODE_PRIVATE);
        val = sp.getString("data", "");

        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                initData();
            }
        },0,3000);

    }

    private void initData() {

        JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "personCenterSet", new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    JSONObject data = jsonObject.getJSONObject("data");
                    String price = data.getString("price");

                    // 显示状态栏通知
                    if(Integer.parseInt(val) > Integer.parseInt(price)){


                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        /**
                         * Oreo不用Priority了，用importance
                         * IMPORTANCE_NONE 关闭通知
                         * IMPORTANCE_MIN 开启通知，不会弹出，但没有提示音，状态栏中无显示
                         * IMPORTANCE_LOW 开启通知，不会弹出，不发出提示音，状态栏中显示
                         * IMPORTANCE_DEFAULT 开启通知，不会弹出，发出提示音，状态栏中显示
                         * IMPORTANCE_HIGH 开启通知，会弹出，发出提示音，状态栏中显示
                         */
                        NotificationChannel channel = new NotificationChannel("0", "com.huike.game", NotificationManager.IMPORTANCE_HIGH);
                        manager.createNotificationChannel(channel);
                    }

                        PendingIntent intent = PendingIntent.getActivity(context,0,new Intent(context, GameActivity20.class),PendingIntent.FLAG_CANCEL_CURRENT);
                        Notification notification = new NotificationCompat
                           .Builder(context,"channel")
                                .setContentText("当前值:" + price +" 阈值:"+val)//*
                                .setContentTitle("阈值管理")//*
                                .setSmallIcon(R.mipmap.ic_launcher_round)//*
                                .setAutoCancel(true)//自动消失
                                .setChannelId("0")
                                .setContentIntent(intent)
                                .build();
                        manager.notify(1,notification);

                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, null);

        queue.add(request);
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View inflate = View.inflate(context, R.layout.game22_set, null);
         editText = inflate.findViewById(R.id.et_game22_val);
         tv_game22_desc = inflate.findViewById(R.id.tv_game22_desc);
        Button but_game22_val = inflate.findViewById(R.id.but_game22_val);

        but_game22_val.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

        String val = editText.getText().toString();
        SharedPreferences price = context.getSharedPreferences("price", Context.MODE_PRIVATE);
        price.edit().putString("data",val).commit();
                tv_game22_desc.setText("当前1-4号小车账户余额告警阈值为"+val+"元");
            }
        });

        SharedPreferences price = context.getSharedPreferences("price", Context.MODE_PRIVATE);
        String data = price.getString("data", "");

        if(data.equals("")){
            tv_game22_desc.setText("当前1~4号小车账户余额告警阈值未设置！");

        }else{
            tv_game22_desc.setText("当前1-4号小车账户余额告警阈值为"+data+"元");

        }

        return inflate;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        timer.cancel();
        timer = null;
    }
}
